package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.LoanBidsDetailMapper;
import com.gxa.p2p.pojo.LoanApplication;
import com.gxa.p2p.pojo.LoanBidsDetail;
import com.gxa.p2p.service.LoanBidsDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("loanBidsDetailService")
@Transactional(readOnly = true,isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS)
public class LoanBidsDetailServiceImpl implements LoanBidsDetailService {

    @Resource(name = "loanBidsDetailMapper")
    private LoanBidsDetailMapper loanBidsDetailMapper;

    /**
     *  查询表中数据总数和带条件的数据总数的方法
     * @return 返回总的数据数
     */
    @Override
    public Integer count(Map<String,Object> map) {
        Integer count = loanBidsDetailMapper.count(map);
        return count;
    }

    /**
     *  分页查询和模糊查询的方法
     * @param map 分页查询和模糊查询的条件
     * @return 返回查询出来的对象
     */
    @Override
    public List<LoanBidsDetail> selectLimitLike(Map<String,Object> map) {
        // 对传入的page和limit进行处理
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer)map.get("limit");
        map.put("page",(page-1)*limit);
        // 对输入的条件进行模糊查询的处理
        if(map.get("realName") != null){
            map.put("realName","%"+map.get("realName")+"%");
        }else if(map.get("phoneNumber") != null){
            map.put("phoneNumber","%"+map.get("phoneNumber")+"%");
        }

        List<LoanBidsDetail> loanBidsDetailList = loanBidsDetailMapper.selectLimitLike(map);

        for (LoanBidsDetail loanBidsDetail : loanBidsDetailList) {
            // 遍历集合,将集合中每个对象中的借款资料取出来,通过split方法进行拆分
            if(loanBidsDetail.getLoanMaterials() != null){
                String[] split = loanBidsDetail.getLoanMaterials().split("\\+");
                // 创建集合对象,将新的路径封装到数组里
                List<String> newFilePath = new ArrayList<>();
                // 遍历字符串数组,将新的路径添加到集合中
                for (String s : split) {
                    newFilePath.add("/p2p/static/uploading/"+s);
                }
                // 将集合赋值给LoanBidsDetail对象中的集合属性
                loanBidsDetail.setFilePath(newFilePath);
            }
        }
        return loanBidsDetailList;
    }

    /**
     *  修改方法
     * @param loanBidsDetail 修改标的状态和审核备注
     * @return 返回受影响的行数
     */
    @Override
    @Transactional(readOnly = false,isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public Integer update(LoanBidsDetail loanBidsDetail) {
        String raiseTime = loanBidsDetail.getRaiseTime();
        // 判断输入的是小时还是天数
        if(loanBidsDetail.getRaiseTime() != null && raiseTime.endsWith("时")) {
            // 取出小时数
            String substring = raiseTime.substring(0,raiseTime.indexOf("小"));
            // 将小时数字符串转换为整型
            int time = Integer.parseInt(substring);
            // 对时间取模得到余下的小时数
            int hour = time%24;
            // 对时间取商得到天数
            int day = time/24;
            // 拼接字符串存入数据库
            loanBidsDetail.setRaiseTime(day+"天"+hour+"时");
        }
        return loanBidsDetailMapper.updateByPrimaryKeySelective(loanBidsDetail);
    }


    // 查询某个用户的放标记录
    @Override
    public List<Object> selectByInfo(Integer id, Integer page, Integer limit) {
        Integer userCount = findUserCount(id);
        List<LoanBidsDetail> loanBidsDetails = loanBidsDetailMapper.selectByInfo(id, (page-1)*limit, limit);
        List<Object> list = new ArrayList<>();
        list.add(userCount);
        list.add(loanBidsDetails);
        return list;
    }

    /**
     * 首页散标/债权信息
     * @param id
     * @return
     */
    @Override
    public List<LoanBidsDetail> findLooseLabel(Integer id) {
        return loanBidsDetailMapper.findLooseLabel(id);
    }

    // 统计某个用户的放标数量
    private Integer findUserCount(Integer userId){
        return loanBidsDetailMapper.findUserCount(userId).getCount();
    }

    /**
     * 查询所有标的信息
     * @param
     * @return 所有查询结果
     */
    @Override
    public List<LoanBidsDetail> findAll() {
        List<LoanBidsDetail> loanBidsDetailAll = loanBidsDetailMapper.findAll();
        return loanBidsDetailAll;
    }

    /**
     * 根据id查询系统中标的信息
     * @return
     */
    @Override
    public LoanBidsDetail findById(Integer id) {

        return loanBidsDetailMapper.findById(id);
    }
    /**
     * 插入借款申请
     * @param
     * @param loanBidsDetail
     * @return
     */
    /*
     * add逻辑的判断:
     * 将网页传递的值封装为LoanApplication类的数据,通过Mapper接口方法进行insert传递出结果数
     * 若添加的结果数大于0则返回true,添加成功
     */
    @Override
    public boolean add(LoanBidsDetail loanBidsDetail) {

        int intger=loanBidsDetailMapper.add(loanBidsDetail);
        if(intger>0)
            return true;
        else return false;

    }

}
